import serviceData from '../../data/config';

//配置文件
var config = require("../../config/basicsConfig.js");
//登录操作
var util = require("../../utils/login.js");

var app = getApp();
Page({
  data: {
    banners: [],
    currentPage: 1,
    // 商品信息
    commoditys: [],
    display: '',
    money: 0,
    uuid: '',
  },

  //商品单选触发器
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const commoditys = this.data.commoditys;
    for (let i = 0, len = commoditys.length; i < len; ++i) {
      if (commoditys[i].id == e.detail.value) {
        commoditys[i].checked = true;
        this.setData({
          money: commoditys[i].price
        });
      } else {
        commoditys[i].checked = false;
      }
    }
    this.setData({
      commoditys
    })
  },

  //页面切换时触发
  onShow() {
    if (this.data.uuid != null && this.data.uuid != '') {
      //console.log("**");
      let token = wx.getStorageSync('token');
      //加载商品信息
      wx.request({
        url: config.basics.website + config.requesturls.getCommoditys,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          'token': token,
          'uuid': this.data.uuid,
        },
        success: (res) => {
          //验证token
          let verificationToken = util.log.verificationToken(res.data);
          if (verificationToken == false) {
            return false;
          }

          //初始化默认选项
          //console.log(3<res.data.datas.length);
          for (let i = 0; i < res.data.datas.length; i++) {
            if (i == 0) {
              res.data.datas[i].checked = true;
              this.setData({
                money: res.data.datas[i].price
              });
            } else {
              res.data.datas[i].checked = false;
            }
          }
          this.setData({
            commoditys: res.data.datas
          });
          //console.log(this.data.commoditys);
        }
      })
    }
  },
  onLoad(param) {
    if (param.uuid == null && param.uuid == '') {
      return false;
    }
    console.log(111, param.uuid);
    // this.setData({ shop_info: app.globalData.shopInfo });
    this.setData({
      banners: serviceData.bannerData,
      uuid: param.uuid,
    });

  },

  /**
   * 查询用户优惠活动
   */
  getactivity: function () {
    //查看优惠活动
    wx.request({
      url: config.basics.website + config.requesturls.getactivity,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        'token': wx.getStorageSync('token'),
      },
      success: (res) => {
        //验证token
        let verificationToken = util.log.verificationToken(res.data);
        if (verificationToken == false) {
          return false;
        }


        if (res.data.datas != null) {
          if (res.data.datas.free > 0) {
            wx.showModal({
              title: '提示',
              content: '您拥有一个免费体验机会',
              success: (res) => {
                if (res.confirm) {
                  console.log('用户点击确定')
                  this.pay();
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else if (res.data.datas.discount > 0) {
            wx.showModal({
              title: '提示',
              content: '您拥有一个6折体验机会',
              success: (res) => {
                if (res.confirm) {
                  console.log('用户点击确定')
                  this.pay();
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '确认支付',
              success: (res) => {
                if (res.confirm) {
                  console.log('用户点击确定')
                  this.pay();
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '确认支付',
            success: (res) => {
              if (res.confirm) {
                console.log('用户点击确定')
                this.pay();
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    });
  },


  //支付动作
  pay: function () {
    let paygoods = null;
    for (let i = 0; i < this.data.commoditys.length; i++) {
      if (this.data.commoditys[i].checked) {
        paygoods = this.data.commoditys[i];
        break;
      }
    }

    //console.log(paygoods);
    wx.showLoading({
      title: '加载中…'
    })
    this.showview();
    //支付
    wx.request({
      url: config.basics.website + config.requesturls.getorder,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        'token': wx.getStorageSync('token'),
        'passId': paygoods.id,
        'uuid': '26004C0009534D5034313920',
      },
      success: (res) => {
        console.log(res);
        //验证token
        let verificationToken = util.log.verificationToken(res.data);
        if (verificationToken == false) {
          return false;
        }

        if (res.data.errCode == 205) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000 //持续的时间
          })
          this.hideview();
          return false;
        } else if (res.data.errCode == 208) {
          console.log(res.data);
          wx.showToast({
            title: '机器连接异常，请稍后再试',
            icon: 'none',
            duration: 2000 //持续的时间
          });
          this.hideview();
          return false;
        }

        //console.log(res);

        //拉起支付
        console.log(res.data.datas);
        wx.requestPayment({
          nonceStr: res.data.datas.nonceStr, //随机字符串
          package: res.data.datas.package, //统一下单接口返回的 prepay_id 参数值
          paySign: res.data.datas.paySign, //签名
          timeStamp: res.data.datas.timeStamp, //时间戳
          signType: res.data.datas.signType,
          success: (res) => {
            wx.hideLoading()
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000 //持续的时间
            })
            this.hideview();
          },
          fail: (res) => {
            wx.hideLoading()
            wx.showToast({
              title: '支付失败，请稍后再试',
              icon: 'none',
              duration: 2000 //持续的时间
            })
            this.hideview();
          }
        })
      }
    })
  },


  //显示
  showview: function () {
    this.setData({
      display: "block"
    })
  },
  //隐藏
  hideview: function () {
    this.setData({
      display: "none"
    })
  },

  /**
   * 二维码识别
   */
  scanCode:function(){
    wx.scanCode({
      success :(res)=>{
        console.log(res.result);
        let start = res.result.indexOf("uuid%3D")+7;
        let end = res.result.indexOf("#");
        let uuid = res.result.slice(start,end);
        console.log(start);
        console.log(end);
        console.log(uuid);
        this.setData({
          uuid:uuid
        });

        this.onShow();
      }
    })
  }
});